<?php
// php最大执行时间设置为：半个小时，php原来默认为30秒，爬不完
ini_set('max_execution_time', '0');
include '../vendor/autoload.php';// 使用composer安装后引入目录
use QL\QueryList;
include_once 'class/Mysql.php';



try {



    foreach ($book_urls as $book_key => $book_urls_value) {
        if ($book_key < $db ) continue;
        echo $db.PHP_EOL;
        //抓取数据
        $data['source_url'] = $book_urls_value;
        $q = QueryList::get($data['source_url']);
        $data['book_name'] = $q->find('#info>h1')->text();

        $bookMysql = Mysql::getMysql('books');
        $book_obj = $bookMysql->fields('book_id,total_pages')->where(['book_name' => $data['book_name']])->find();
        if(!empty($book_obj)) continue;


        $data['cover'] = $q->find('#fmimg img')->attr('src');
        $data['author_name'] = $q->find('#info>p:eq(0)')->text();
        $data['cate_name'] = $q->find('#info>p:eq(2)')->text();
        $data['book_status'] = $q->find('#info>p:eq(4)')->text();
        $data['last_page'] = $q->find('#info>p:eq(5)')->text();
        $data['describe'] = $q->find('#intro>p:eq(1)')->text();
        $pages = $q->find("#list a")->texts();
        $urlList = $q->find("#list a")->attrs('href');
        //处理数据
        $data['book_name'] = trim(str_ireplace("无弹窗", '', $data['book_name']));
        $data['author_name'] = trim(explode("：", $data['author_name'])[1]);
        $data['cate_name'] = trim(explode("：", $data['cate_name'])[1]);
        $data['book_status'] = trim(explode("：", $data['book_status'])[1]);
        $data['last_page'] = trim(explode("：", $data['last_page'])[1]);

        //创建分类
        $cateMysql = Mysql::getMysql('book_cate');
        $cate_obj = $cateMysql->fields('cate_id,book_num')->where(['cate_name' => $data['cate_name']])->find();
        if (empty($cate_obj)) {
            $cate_id = $cateMysql->add([
                'cate_name' => $data['cate_name'],
                'book_num' => 1,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        } else {
            $cate_id = $cate_obj->cate_id;
            $book_num = $cate_obj->book_num;
            $cateMysql->where(['cate_id' => $cate_id])->update([
                'book_num' => $book_num + 1,
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        }


        //创建作者
        $authorMysql = Mysql::getMysql('author');
        $author_obj = $authorMysql->fields('author_id,book_num')->where(['author_name' => $data['author_name']])->find();
        if (empty($author_obj)) {
            $author_id = $authorMysql->add([
                'author_name' => $data['author_name'],
                'book_num' => 1,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        } else {
            $author_id = $author_obj->author_id;
            $book_num = $author_obj->book_num;
            $authorMysql->where(['author_id' => $author_id])->update([
                'book_num' => $book_num + 1,
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        }

        //创建书籍ID
        $bookMysql = Mysql::getMysql('books');
        $book_obj = $bookMysql->fields('book_id,total_pages')->where(['book_name' => $data['book_name']])->find();
        if (empty($book_obj)) {
            $book_id = $bookMysql->add([
                'book_name' => $data['book_name'],
                'cover' => $data['cover'],
                'author_id' => $author_id,
                'cate_id' => $cate_id,
                'book_status' => $data['book_status'],
                'describe' => $data['describe'],
                'last_page' => $data['last_page'],
                'total_pages' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        } else {
            $book_id = $book_obj->book_id;
            $total_pages = $book_obj->total_pages;
            $bookMysql->where(['book_id' => $book_id])->update([
                'total_pages' => $total_pages + 1,
                'update_time' => date('Y-m-d H:i:s', time())
            ]);
        }
        //处理数据和数据源的绑定关系
        $bookSourcMysql = Mysql::getMysql('book_source');
        $book_sourc_obj = $bookSourcMysql->query("SELECT book_id,source_url FROM book_source WHERE book_id='" . $book_id . "' AND source_url = '" . $data['source_url'] . "' AND status = 1 LIMIT 1");
        $book_sourc_obj = $book_sourc_obj->fetch();
        if (empty($book_sourc_obj)) {
            $book_source_id = $bookSourcMysql->add([
                'book_id' => $book_id,
                'source_url' => $data['source_url'],
                'status' => 1
            ]);
        }

        //处理章节
//            $bookPageSourceMysql = Mysql::getMysql('book_page_source');
//            $bookPageMysql = Mysql::getMysql('book_pages');
//            foreach ($pages as $k => $v) {
//                $data['pageList'][$k]['pageName'] = $v;
//                $data['pageList'][$k]['pageUrl'] = pathinfo($data['source_url'])['dirname'] . '/' . $urlList[$k];
//                //存储章节源
//                $book_page_source_obj = $bookPageSourceMysql->query("SELECT id,status FROM book_page_source WHERE book_id='" . $book_id . "' AND source_url = '" . $data['source_url'] . "' AND status IN (1,2) LIMIT 1");
//                $book_page_source_obj = $book_page_source_obj->fetch();
//                if (empty($book_page_source_obj)) {
//                    $page_source_id = $bookPageSourceMysql->add([
//                        'book_id' => $book_id,
//                        'page_source_url' => $data['pageList'][$k]['pageUrl'],
//                        'status' => 1
//                    ]);
//                }
//                //抓取章节内容
//                $p = QueryList::get($data['pageList'][$k]['pageUrl']);
//                $p->find('.bottem')->remove();
//                $data['pageList'][$k]['content'] = $p->find('#content')->text();
//                //处理数据
//                $data['pageList'][$k]['content'] = str_ireplace("hhh5804171", '', $data['pageList'][$k]['content']);
//                $data['pageList'][$k]['content'] = array_map(function ($pp) {
//                    return "<p>" . trim($pp) . "</p>";
//                }, explode("\n", $data['pageList'][$k]['content']));
//                $data['pageList'][$k]['content'] = implode('', $data['pageList'][$k]['content']);
//
//                echo $data['pageList'][$k]['pageName'] . PHP_EOL;
//
//                //获取章节数
//                $maxPageNumobj = $bookPageMysql->query("SELECT MAX(page_num) as page_num FROM book_pages WHERE book_id='" . $book_id . "'");
//                $maxPageNum = $maxPageNumobj->fetch();
//                if (empty($maxPageNum)) {
//                    $maxPageNum = 1;
//                } else {
//                    $maxPageNum = $maxPageNum['page_num'] + 1;
//                }
//
//                //章节入库
//                $book_page_obj = $bookPageMysql->query("SELECT page_id FROM book_pages WHERE book_id='" . $book_id . "' AND page_name = '" . $data['pageList'][$k]['pageName'] . "' AND status =1 LIMIT 1");
//                $book_page_obj = $book_page_obj->fetch();
//
//                if (empty($book_page_obj)) {
//                    $page_id = $bookPageMysql->add([
//                        'page_name' => $data['pageList'][$k]['pageName'],
//                        'book_id' => $book_id,
//                        'content' => $data['pageList'][$k]['content'],
//                        'page_num' => $maxPageNum,
//                        'create_time' => date('Y-m-d H:i:s', time()),
//                        'update_time' => date('Y-m-d H:i:s', time())
//                    ]);
//
//                    //更新章节抓取源
//                    $page_source_id = $bookPageSourceMysql->where(['id' => $page_source_id])->update([
//                        'page_id' => $page_id,
//                        'status' => 2
//                    ]);
//                }
//
//                unset($p);
//            }

        //更新时间文件
        $db += 1;
        file_put_contents('db/book.db', $db);
        break;
    }

} catch (Exception $e) {
    $exceptionlog = '文件 [' . $e->getFile() . '] 第 ' . $e->getLine() . ' 行出现未处理的异常' . PHP_EOL;
    $exceptionlog .= '代码:' . $e->getCode() . PHP_EOL;
    $exceptionlog .= '信息:' . $e->getMessage() . PHP_EOL;
    $exceptionlog .= '跟踪信息：' . $e->getTraceAsString();
    //记录错误
    error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/' . date('Y-m-d') . '_' . 'exception.log');

}








?>
